package org.inote.controller;

import com.github.pagehelper.PageInfo;
import org.inote.common.StatusConst;
import org.inote.pojo.tablePojo.Comment;
import org.inote.pojo.tablePojo.User;
import org.inote.pojo.voPojo.CommentVo;
import org.inote.service.CommentService;
import org.inote.service.NoteService;
import org.inote.utils.StringFormUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;


@Controller
@RequestMapping("/comment")
public class CommentController {

    /**
     * 变量名：评论最大长度
     */
    public static final int MAX_LENGTH = 70;

    /**
     * 执行结果：参数长度过长
     */
    public static final String LENGTH_OVER = "lengthOver";

    CommentService commentService;
    @Autowired
    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }

    /**
     *
     * @param content
     * @param noteId
     * @param session
     * @return
     * 1."nullVar"：参数存在空值
     * 2."logOff"：未登录
     * 3."false"：添加失败
     * 4."true"：评论成功
     * 5."lengthOver"：评论过长
     */
    @ResponseBody
    @RequestMapping("/addComment")
    public String addComment(String content, Integer noteId, HttpSession session){
        User user = (User) session.getAttribute(UserController.USER_INFO);
        if (user==null){
            return UserController.LOG_OFF;
        }
        if (StringFormUtil.isEmpty(content)){
            return StatusConst.NULL_VAR;
        }
        if (!StringFormUtil.stringLengthForm(content, MAX_LENGTH)){
            return LENGTH_OVER;
        }
        Comment comment = new Comment(null, user.getUserId(), content, noteId, new Date());
        boolean result = commentService.addComment(comment);
        if (!result){
            return StatusConst.FALSE;
        }
        return StatusConst.TRUE;
    }

    /**
     * 获取某一条笔记的所有评论
     * @param commentNoteId
     * @param pageNum
     * @return
     * 1.commentPageInfo：某一页评论
     */
    @ResponseBody
    @RequestMapping("/getCommentByNoteIdByPage")
    public PageInfo<CommentVo> getCommentByNoteIdByPage(Integer commentNoteId, @RequestParam(defaultValue = "1") Integer pageNum){
        PageInfo<CommentVo> commentPageInfo = commentService.queryNoteCommentsByPage(commentNoteId, pageNum);
        System.out.println(commentPageInfo);
        return commentPageInfo;
    }

    /**
     * 删除评论
     * @param commentId
     * @return
     * 1."nullVar"：参数存在空值
     * 2."false"：评论删除失败
     * 3."true"：评论删除成功
     */
    @ResponseBody
    @RequestMapping("/deleteComment")
    public String deleteComment(Integer commentId,Integer noteId){
        if (commentId==null){
            return StatusConst.NULL_VAR;
        }
        boolean result = commentService.deleteComment(commentId,noteId);

        if (!result){
            return StatusConst.FALSE;
        }
        return StatusConst.TRUE;
    }
}
